package com.yiwei.zhh.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.event.OnHomeEventMsg;
import com.yiwei.zhh.fragment.FragmentAdapter;
import com.yiwei.zhh.fragment.HelpFragment;
import com.yiwei.zhh.fragment.MessageFragment;
import com.yiwei.zhh.util.DisplayUtil;
import com.yiwei.zhh.util.StatusBarCompat;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ˙帮助界面
 * Created by Master.Dong on 16/4/10.
 */
public class HelpActivity extends BaseActivity implements View.OnClickListener {

    private static final int MESSAGE = 0;
    private static final int HELP = 1;

    private ViewPager mPageVp;
    private FragmentAdapter mFragmentAdapter;
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();

    private final static int pageSize = 2;

    /**
     * Tab显示内容TextView
     */
    private TextView msgBtn, helpBtn;

    /**
     * Tab的那个引导线
     */
    private ImageView mTabLineIv;
    /**
     * Fragment
     */
    private HelpFragment helpFragment;
    private MessageFragment messageFragment;
    /**
     * ViewPager的当前选中页
     */
    private int currentIndex;
    /**
     * 屏幕的宽度
     */
    private int screenWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.help_activity);
        baseInitView(this, "HelpActivity", Const.ViewType.ACTIVITY);
        initView();



    }

    private void initView() {

        setTitleText(getResources().getString(R.string.service_center));
        setBackIntent();

        mPageVp = (ViewPager) findViewById(R.id.viewPager);
        mTabLineIv = (ImageView) findViewById(R.id.id_tab_line_iv);
        helpBtn = (TextView) findViewById(R.id.help_tv);
        msgBtn = (TextView) findViewById(R.id.msg_tv);
        ((LinearLayout) findViewById(R.id.msg_ll)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.help_ll)).setOnClickListener(this);

        initFragment();

        int type = 1;
        try {
            type = getIntent().getIntExtra("type", 1);
            setCurPaper(type);
        } catch (Exception e) {
            e.printStackTrace();
        }

        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                .getLayoutParams();
        lp.width = screenWidth / pageSize - DisplayUtil.dip2px(this, 40);
        if (type == 1) lp.leftMargin = type
                * (screenWidth / pageSize) + DisplayUtil.dip2px(this, 20);
        mTabLineIv.setLayoutParams(lp);

    }

    private void initFragment() {
        messageFragment = new MessageFragment();
        helpFragment = new HelpFragment();

        mFragmentList.add(messageFragment);
        mFragmentList.add(helpFragment);

        mFragmentAdapter = new FragmentAdapter(HelpActivity.this.getSupportFragmentManager(), mFragmentList);
        mPageVp.setAdapter(mFragmentAdapter);
        mPageVp.setCurrentItem(0);

        mPageVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }

            /**
             * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
             * offsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset,
                                       int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();
                /**
                 * 利用currentIndex(当前所在页面)和position(下一个页面)以及offset来
                 * 设置mTabLineIv的左边距 滑动场景：
                 * 记3个页面,
                 * 从左到右分别为0,1,2
                 * 0->1; 1->2; 2->1; 1->0
                 */

                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / pageSize) + currentIndex
                            * (screenWidth / pageSize)) + DisplayUtil.dip2px(getApplicationContext(), 20);

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - offset)
                            * (screenWidth * 1.0 / pageSize) + currentIndex
                            * (screenWidth / pageSize)) + DisplayUtil.dip2px(getApplicationContext(), 20);

                }
//                else if (currentIndex == 1 && position == 1) // 1->2
//                {
//                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / pageSize) + currentIndex
//                            * (screenWidth / pageSize));
//                } else if (currentIndex == 2 && position == 1) // 2->1
//                {
//                    lp.leftMargin = (int) (-(1 - offset)
//                            * (screenWidth * 1.0 / pageSize) + currentIndex
//                            * (screenWidth / pageSize));
//                }

//                else if (currentIndex == 2 && position == 2) {
//                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / pageSize) + currentIndex
//                            * (screenWidth / pageSize));
//                }
//                else if (currentIndex == 3 && position == 2) // 2->1
//                {
//                    lp.leftMargin = (int) (-(1 - offset)
//                            * (screenWidth * 1.0 / 3) + currentIndex
//                            * (screenWidth / 3));
//                }
                mTabLineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                switch (position) {
                    case 0:
                        msgBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.main_color));
                        break;
                    case 1:
                        helpBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.main_color));
                        break;
                }
                currentIndex = position;
            }
        });
    }

    /**
     * 设置滑动条的宽度为屏幕的1/4(根据Tab的个数而定)
     */
    private void initTabLineWidth() {
        int type = 1;
        try {
            type = getIntent().getIntExtra("type", 1);
            setCurPaper(type);
        } catch (Exception e) {
            e.printStackTrace();
        }

        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                .getLayoutParams();
        lp.width = screenWidth / pageSize;
        if (type == 1) lp.leftMargin = type
                * (screenWidth / pageSize);
        mTabLineIv.setLayoutParams(lp);
    }

    /**
     * 重置颜色
     */
    private void resetTextView() {
        msgBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.v3_text_one));
        helpBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.v3_text_one));
    }

    /**
     * 设置当前页
     */
    private void setCurPaper(int pos) {
        resetTextView();
        mPageVp.setCurrentItem(pos);
        switch (pos) {
            case 0:
                msgBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.main_color));
                break;
            case 1:
                helpBtn.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.main_color));
                break;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.help_ll:
                setCurPaper(1);
                break;
            case R.id.msg_ll:
//                ToastUtil.makeText(this, "敬请期待...", Toast.LENGTH_SHORT).show();
                setCurPaper(0);
                break;
        }
    }

    private void sendBroadcast() {
        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.UPDATE);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sendBroadcast();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
